// 高级配置管理
const env = import.meta.env.MODE || 'development';

// 从环境变量读取配置，如果没有则使用默认值
const getEnvVar = (key, defaultValue) => {
  return import.meta.env[key] || defaultValue;
};

const config = {
  development: {
    POCKETBASE_URL: getEnvVar('VITE_POCKETBASE_URL_DEV', 'http://172.27.235.239:7070'),
    API_BASE_URL: getEnvVar('VITE_API_BASE_URL_DEV', 'http://172.27.235.239:7070/api'),
    DEBUG: true,
    LOG_LEVEL: 'debug',
  },
  production: {
    POCKETBASE_URL: getEnvVar('VITE_POCKETBASE_URL_PROD', 'http://xiong.plus:7070'),
    API_BASE_URL: getEnvVar('VITE_API_BASE_URL_PROD', 'http://xiong.plus:7070/api'),
    DEBUG: false,
    LOG_LEVEL: 'error',
  },
  test: {
    POCKETBASE_URL: getEnvVar('VITE_POCKETBASE_URL_TEST', 'http://localhost:8090'),
    API_BASE_URL: getEnvVar('VITE_API_BASE_URL_TEST', 'http://localhost:8090/api'),
    DEBUG: true,
    LOG_LEVEL: 'debug',
  }
};

// 获取当前环境的配置
export const getConfig = () => {
  return config[env] || config.development;
};

// 导出配置常量
export const {
  POCKETBASE_URL,
  API_BASE_URL,
  DEBUG,
  LOG_LEVEL
} = getConfig();

// 导出环境信息
export const isDevelopment = env === 'development';
export const isProduction = env === 'production';
export const isTest = env === 'test';

// 日志函数
export const log = (message, level = 'info') => {
  if (DEBUG || level === 'error') {
    const timestamp = new Date().toISOString();
    console.log(`[${timestamp}] [${level.toUpperCase()}] ${message}`);
  }
};

// 初始化时输出配置信息
log(`当前环境: ${env}`, 'info');
log(`PocketBase URL: ${POCKETBASE_URL}`, 'info');
log(`API Base URL: ${API_BASE_URL}`, 'info'); 